using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace UmlControlLibrary.AStar
{
    public class AStarNode:IComparable
    {
        public enum NodeStatus {Free, Start, Goal, Edge, Block};

        public const int NodeSize = 25;

        public int F = 0;
        public int G = 0;
        public int H = 0;

        public int X;
        public int Y;

        public bool walked = false;
        public bool selected = false;

        public AStarNode[,] Map;

        public NodeStatus Status = NodeStatus.Free;

        public AStarNode Parent = null;

        public ArrayList ParentCandidates = new ArrayList();

        public int GetCost(AStarNode fromNode, AStarNode previous)
        {
            if (Status == NodeStatus.Block)
                return 10000;

            if (Status == NodeStatus.Edge)
                return 100;

            if ((previous != null) && (!((X - fromNode.X == fromNode.X - previous.X) && (Y - fromNode.Y == fromNode.Y - previous.Y))))
                return 20;

            return 1;
        }

        public int GetCost(AStarNode fromNode)
        {
            return GetCost(fromNode, fromNode.Parent);
        }

        public List<AStarNode> GetNeighbours()
        {
            List<AStarNode> res = new List<AStarNode>();
            if (X > 0) res.Add(Map[X - 1, Y]);
            if (Y > 0) res.Add(Map[X, Y - 1]);
            if (X < Map.GetLength(0) - 1) res.Add(Map[X + 1, Y]);
            if (Y < Map.GetLength(1) - 1) res.Add(Map[X, Y + 1]);
            return res;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj is AStarNode)
            {
                AStarNode o = (AStarNode)obj;
                if (o.F < F) return 1;
                if (o.F > F) return -1;
            }
            return 0;
        }

        #endregion
    }
}
